﻿<?php
/**
 * This is the file to define Home.
 * 
 * @author	Justin Yoo
 * @version	0.0.1.0001
 * @package	KYR.Modules
 * 
 */

//	Checks if this file is called within the framework; otherwise terminates.
if (!defined("IS_FRAMEWORK") || !IS_FRAMEWORK)	header("Location: http://".$_SERVER["HTTP_HOST"]."/error/404");

//	Imports relevant files.
require_once CORE_PATH."KYR.Core.BaseModule.php";

/**
 * This is class Home that processes to list, view and write contents.
 *
 * Processes to list, view and write contents.
 * 
 * @author	Justin Yoo
 * @package	KYR.Modules
 * 
 */
class Home extends BaseModule
{
	/**
	 * Initiates the Home class.
	 *
	 * @access	public
	 * @param	Request	$request	Request object that contains page request details.
	 * @param	int		$statusCode	Page response code.
	 * @exception					Throws an Exception while checking the module.
	 *
	 */
	public function __construct($request, $statusCode = 200)
	{
		//	Initiates the parent class BaseModule.
		parent::__construct($request, $statusCode);
		$this->moduleName	= __CLASS__;
		$this->error		= null;

		//	Checks if the module is registered or not
		if ($this->statusCode == 200)
		{
			try
			{
				$this->CheckModule_();
				if (!$this->existModule)
					throw new Exception("Module not found");
			}
			catch (Exception $ex) { throw $ex; }
		}
		else
			$this->existModule = true;
	}
	
	/**
	 * Processes the module.
	 *
	 * @access	public
	 * @exception	Throws an Exception while processing the module and rendering templates.
	 *
	 */
	public function ProcessModule()
	{
		try
		{
			//	Sets error messages to log file.
			if (isset($this->error))
			{
				$msg = "-- ".$this->error->getMessage()." --\n";
				$msg .= "\tat line ".$this->error->getLine()."\n";
				$msg .= "\ton file ".$this->error->getFile()."\n";
				$msg .= "\tat page ".BASE_URL.$this->request->uri."\n\n";
				$msg .= $this->error->getTraceAsString()."\n";
				$msg .= method_exists($this->error, "getQuery") ? "\n".$this->error->getQuery()."\n" : null;
				
				$this->variables_["message"]	= $msg;
			}
			$this->ProcessModule_();
			$this->RenderTemplates_();
		}
		catch (Exception $ex)
		{
			$this->RenderTemplates_($ex);
		}
	}
	
	/**
	 * Renders the templates corresponding to the module.
	 *
	 * @access	protected
	 * @param	Exception	$ex	Exception.
	 *
	 */
	protected function RenderTemplates_($ex = null)
	{
		$controls	= array();
		$exception	= isset($this->error) ? $this->error : (isset($ex) ? $ex : null);

		try
		{
			foreach ($this->templates_ as $template)
			{
				if ($this->request->actionStep == $template["actionStep"])
				{
					$controls = $this->variables_["controls"][$template["template"]];

					$filename = TEMPLATE_PATH."KYR.Templates.".$template["template"].".php";
					if (file_exists($filename))
						$contents .= file_get_contents($filename);
					else
						throw new Exception("Template not found");
				}
			}
			//Debug::PrintScreen("pre", $contents);
			$this->ReplaceTemplateTags_($contents, $controls);
			echo $contents;
		}
		catch (Exception $ex) { throw $ex; }

//		foreach ($this->templates_ as $template)
//		{
//			$controls = $this->variables_["controls"][$template];
//
//			$filename = TEMPLATE_PATH."KYR.Templates.".$template.".php";
//			if (file_exists($filename))
//				require_once $filename;
//			else
//				require_once TEMPLATE_PATH."KYR.Templates.ContentView.php";
//		}
	}
}
?>
